package com.special.threademo.thread;


/**
 * @author liuYC
 * @ClassName ThreadDemo
 * @Description TODO
 * @date 2021/4/26 7:38
 */
class ThreadDemoDeason extends Thread {
    @Override
    public void run() {
        System.out.println("thread execute  ^^^^^^");
    }

    static class RunDemo implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
//        守护线程作为⼀个服务线程，没有服务对象就没有必要继续运⾏了
//        在线程启动前设置为守护线程，⽅法是 setDaemon(boolean on)
//2. 使⽤守护线程不要访问共享资源(数据库、⽂件等)，因为它可能会在任何时候就挂掉了
        RunDemo target = new RunDemo();
        Thread runnable = new Thread(target, "runnable-name-1");
        Thread runnable2 = new Thread(target, "runnable-name-2");
        runnable2.setDaemon(true);
        runnable.start();
        runnable2.start();
        System.out.println(Thread.currentThread().getName());
    }
}
